
<div class="widget-container">
    <?php
    $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
        'id' => 'product-form',
        'htmlOptions' => array(
            'enctype' => 'multipart/form-data',
            'class' => 'form-horizontal'
        ),
    ));
    ?>
    <div class="row-fluid">
        <div class="span2">
            <div class="widget-box">
                <div class="widget-title">
                    <span class="icon">
                        <i class="icon-refresh"></i>
                    </span>
                    <h5>Quản lý sản phẩm</h5>
                </div>

                <div class="widget-content nopadding">
                    <table class="table table-bordered">
                        <tbody>
                            <?php
                            if ($model->getScenario() == 'update') {
                                $delete = '<tr><td>' . Chtml::link('Xóa sản phẩm', array('product/delete/' . $model->id)) . '</td></tr>';
                                $addProduct = '<tr><td>' . Chtml::link('Thêm mới sản phẩm', array('product/create')) . '</td></tr>';
                                $success = '<tr><td>Ban đang ở trang cập nhập sản phẩm</td></tr>';
                            } else {
                                $delete = '';
                                $addProduct = '';
                                $success = '<tr><td>Ban đang ở trang thêm mới sản phẩm</td></tr>';
                            }
                            ?>
                            <tr><td><?php echo Chtml::link('Danh mục sản phẩm', array('productCategory/')) ?></td></tr>
                            <tr><td><?php echo Chtml::link('Thêm mới danh mục', array('productCategory/create')) ?></td></tr>
                            <tr><td><?php echo Chtml::link('Danh sách sản phẩm', array('product/')) ?></td></tr>
                            <?php echo $addProduct ?>
                            <?php echo $delete ?>
                            <?php echo $success ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="span10">
            <div class="span9">
                <div class="widget-box">
                    <div class="widget-title">
                        <span class="icon">
                            <i class="icon-refresh"></i>
                        </span>
                        <h5>Chi tiết</h5>
                    </div>
                    <div class="widget-content nopadding">
                        <div class="control-group">
                            <?php echo $form->labelEx($model, 'name', array('class' => 'control-label')); ?>
                            <div class="controls">
                                <?php
                                echo $form->textField($model, 'name', array('class' => 'span8'));
                                ?>
                                <?php echo $form->error($model, 'name'); ?>
                            </div>
                        </div>
                        <div class="control-group">
                            <?php echo $form->labelEx($model, 'alias', array('class' => 'control-label')); ?>
                            <div class="controls">
                                <?php
                                echo $form->textField($model, 'alias', array('class' => 'span8'));
                                ?>
                                <?php echo $form->error($model, 'alias'); ?>
                            </div>
                        </div>
                        <div class="control-group">
                            <?php echo $form->labelEx($model, 'price', array('class' => 'control-label')); ?>
                            <div class="controls">
                                <?php
                                echo $form->textField($model, 'price', array('class' => 'span4'));
                                ?>
                                <span class="resultPrice">
                                    <?php
                                    if ($model->getScenario() == 'update')
                                        echo number_format($model->price, 0, ",", ".") . ' VND'
                                        ?>
                                </span>
                                <?php echo $form->error($model, 'price'); ?>
                            </div>
                        </div>
                        <div class="control-group">
                            <?php echo $form->labelEx($model, 'sale', array('class' => 'control-label')); ?>
                            <div class="controls" id="demo-column">
                                <?php
                                echo $form->textField($model, 'sale', array('class' => 'span4'));
                                ?>
                                <span class="resultSale">
                                    <?php
                                    if ($model->getScenario() == 'update')
                                        echo number_format($model->sale, 0, ",", ".") . ' VND'
                                        ?>
                                </span>
                                <?php echo $form->error($model, 'sale'); ?>
                            </div>

                        </div>
                        <div class="control-group">
                            <?php echo $form->labelEx($model, 'state', array('class' => 'control-label')); ?>
                            <div class="controls">
                                <?php
                                echo $form->textField($model, 'state', array('class' => 'span8'));
                                ?>
                                <?php echo $form->error($model, 'state'); ?>
                            </div>
                        </div>

                    </div>
                </div>
                <div class="widget-box">
                    <div class="widget-title">
                        <?php echo $form->labelEx($model, 'content', array('class' => 'control-label-widget')); ?>
                    </div>
                    <div class="widget-content nopadding">
                        <div class="control-group control-editor">
                            <?php
                            $this->widget('ext.widgets.xheditor.XHeditor', array(
                                'model' => $model,
                                'modelAttribute' => 'content',
                                'config' => array(
                                    'id' => 'xheditor_1',
                                    'tools' => 'mfull', // mini, simple, mfull, full or from XHeditor::$_tools, tool names are case sensitive
                                    'skin' => 'default', // default, nostyle, o2007blue, o2007silver, vista
                                    'width' => '100%',
                                    'height' => '300px',
//                                    'loadCSS' => XHtml::cssUrl('editor.css'),
                                    'upImgUrl' => $this->createUrl('request/uploadFile'),
                                    'upImgExt' => 'jpg,jpeg,gif,png',
                                ),
                            ));
                            ?>
                            <?php echo $form->error($model, 'content'); ?>

                        </div>
                    </div>
                </div>
            </div>
            <div class="span3">
                <div class="widget-box">
                    <div class="widget-title">
                        <span class="icon">
                            <i class="icon-refresh"></i>
                        </span>
                        <h5>Hình minh họa</h5>
                    </div>
                    <div class="control-group control-mar">
                        <?php echo $form->fileField($model, 'image', array('class' => 'file_upload')); ?>
                        <?php echo $form->error($model, 'image'); ?>

                    </div>
                </div>
                <div class="widget-box">
                    <div class="widget-title">
                        <span class="icon">
                            <i class="icon-refresh"></i>
                        </span>
                        <h5>Danh mục</h5>
                    </div>
                    <div class="control-group control-mar">
                        <?php
                        $pcfk = $this->findCategory($model->category_id);

                        if (!empty($category)) {
                            foreach ($category as $key => $value) {
                                ?>
                                <label class="checkbox">
                                    <?php
                                    if (!empty($pcfk)) {
                                        $t = 0;
                                        foreach ($pcfk as $v) {
                                            if ($v->id == $value->id) {
                                                $t = 1;
                                            }
                                        }
                                        if ($t == 0) {
                                            ?> 
                                            <input type="checkbox" name="Product[category_id][]" value="<?php echo $value->id ?>"><?php echo $value->name ?>
                                        <?php } else { ?>
                                            <input type="checkbox" name="Product[category_id][]" checked="checked" value="<?php echo $value->id ?>"><?php echo $value->name ?>
                                            <?php
                                        }
                                    } else {
                                        ?>
                                        <input type="checkbox" name="Product[category_id][]" value="<?php echo $value->id ?>"><?php echo $value->name ?>
                                    <?php } ?>
                                </label>
                                <?php
                                $item = $this->findRoot($value->id);
                                foreach ($item as $val) {
                                    if (!empty($pcfk)) {
                                        $vi = 0;
                                        foreach ($pcfk as $vitem) {
                                            if ($vitem->id == $val->id)
                                                $vi = 1;
                                        }
                                        if ($vi == 0) {
                                            ?>
                                            <label class="checkbox" style="padding-left: 40px"><input type="checkbox" name="Product[category_id][]" value="<?php echo $val->id ?>"><?php echo $val->name ?></label>
                                        <?php } else { ?>
                                            <label class="checkbox" style="padding-left: 40px"><input type="checkbox" checked="checked" name="Product[category_id][]" value="<?php echo $val->id ?>"><?php echo $val->name ?></label>
                                            <?php
                                        }
                                    } else {
                                        ?>
                                        <label class="checkbox" style="padding-left: 40px"><input type="checkbox" name="Product[category_id][]" value="<?php echo $val->id ?>"><?php echo $val->name ?></label>
                                        <?php
                                    }
                                }
                            }
                        }
                        ?>
                        <?php echo $form->error($model, 'category_id'); ?>
                    </div>
                </div>
                <div class="widget-box">
                    <div class="widget-title">
                        <span class="icon">
                            <i class="icon-refresh"></i>
                        </span>
                        <h5>Đặc biệt</h5>
                    </div>
                    <div class="control-group control-mar">
                        <?php
                        $sub = explode(',', $model->product_sub);
                        $sptk = 0;
                        $spnew = 0;
                        if (!empty($sub)) {
                            foreach ($sub as $sub) {
                                if ($sub == Product::SPTK)
                                    $stb = Product::SPTK;
                                if ($sub == Product::SPNEW)
                                    $sh = Product::SPNEW;
                            }
                        }
                        ?>
                        <label class="checkbox">
                            <?php
                            if ($sptk == Product::SPTK) {
                                ?>
                                <input type="checkbox" name="product_sub[]" checked="checked" value="<?php echo Product::SPTK ?>">Sản phẩm trong kho
                            <?php } else { ?>
                                <input type="checkbox" name="product_sub[]" value="<?php echo Product::SPTK ?>">Sản phẩm trong kho
                            <?php } ?>
                        </label>
                        <label class="checkbox">
                            <?php
                            if ($spnew == Product::SPNEW) {
                                ?>
                                <input type="checkbox" name="product_sub[]" checked="checked" value="<?php echo Product::SPNEW ?>">Sản phẩm mới nhập
                            <?php } else { ?>
                                <input type="checkbox" name="product_sub[]" value="<?php echo Product::SPNEW ?>">Sản phẩm mới nhập
                            <?php } ?>

                        </label>
                    </div>
                </div>
                <div class="widget-box">
                    <div class="widget-title">
                        <span class="icon">
                            <i class="icon-refresh"></i>
                        </span>
                        <h5>Tags</h5>
                    </div>
                    <div class="control-group control-mar">
                        <?php
                        $this->widget('CAutoComplete', array(
                            'model' => $model,
                            'attribute' => 'tags',
                            'url' => array('ajax/suggestTags'),
                            'multiple' => true,
                            'htmlOptions' => array('size' => 50),
                        ));
                        ?>
                        <?php
                        echo $form->error($model, 'tags');
                        ?>
                        <a href="javascript:void(0)" class="colorboxUpload btn btn-primary">Upload</a>
                        
                    </div>
                </div>
            </div>
        </div>
        <div class="span12">
            <div class="form-actions">
                <?php echo CHtml::submitButton('Luu', array('class' => 'btn btn-success')); ?>

            </div>
        </div>

        <?php
        $this->endWidget();
        $cs = Yii::app()->getClientScript();
        $cs->registerScript('colorboxUpload', '$("a.colorboxUpload").live("click",function(){      
                                                                            $(".colorboxUpload").colorbox({
                                                                                href:"' . Yii::app()->createUrl('admincp/product/image') . '",
                                                                                     opacity: 0,
                                                                                        overlayClose:false,
                                                                                        fixed:true,
                                                                                data:{
                                                                                    "YII_CSRF_TOKEN":"' . Yii::app()->request->csrfToken . '"},
                                                                                title:"",
                                                                                type:"POST",
                                                                                width:"910px", 
                                                                                height:"550px",
                                                                                rel:"nofollow"
                                                                            });
                                                                        });
                                                                            
                                                                ', CClientScript::POS_END
        );
        ?>

    </div><!-- form -->

    <script>
        $(function() {
            $('#Product_name').keyup(function() {
                title = $('#Product_name').val();
                $('#Product_alias').val(convertToSlug(title));
            });
        });
        $(function() {
            $('#Product_sale').keyup(function() {
                sale = $('#Product_sale').val();
                $('.resultSale').html(FormatToCurrency(sale));
            });
        });
        $(function() {
            $('#Product_price').keyup(function() {
                price = $('#Product_price').val();
                $('.resultPrice').html(FormatToCurrency(price));
            });
        });
    </script>